package com.hjx.movie.core.system.controller.admin;

import com.hjx.movie.common.utils.Result;
import com.hjx.movie.common.utils.ResultType;
import com.hjx.movie.core.system.service.admin.IRoleService;
import com.hjx.movie.persistence.system.model.IvyRole;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Comparator;
import java.util.List;

/**
 * <p>Title:</p>
 * <p>Description:</p>
 *
 * @author hjx
 * @date 2021/1/24 - 15:34
 */
@Slf4j
@RestController
@RequestMapping("/role")
public class RoleController {
    @Resource
    IRoleService roleService;



    @GetMapping("/findAll")
    public Result findAllRole(@RequestParam(required = false,value = "sort")String sortType){
        List<IvyRole> allRole = roleService.findAllRole();
        if (StringUtils.isNotBlank(sortType)&&sortType.equals("sort")){
            allRole.sort(Comparator.comparing(IvyRole::getSort));
        }else {
            allRole.sort(Comparator.comparing(IvyRole::getRoleId));
        }
        return Result.success(allRole);
    }

    @GetMapping("/queryLike")
    public Result queryRoleLike(@RequestParam("roleLike") String roleLike,
                                @RequestParam(required = false,value = "sort")String sortType){
        List<IvyRole> roleLikeList = roleService.queryRoleLike(roleLike);
        if (StringUtils.isNotBlank(sortType)&&sortType.equals("sort")){
            roleLikeList.sort(Comparator.comparing(IvyRole::getSort));
        }else {
            roleLikeList.sort(Comparator.comparing(IvyRole::getRoleId));
        }
        return Result.success(roleLikeList);
    }

    @PostMapping("/add")
    public Result addRole(@RequestBody @Valid IvyRole role){
        log.debug("新增角色方法接收到的入参为:{}",role);
        if (roleService.addRole(role)) {
            return Result.success("新增角色成功");
        }
        return Result.error(ResultType.USER_INPUT_ERROR,"新增角色失败,入参不符合规则,请检查入参");
    }

    @PostMapping("/update")
    public Result updateRole(@RequestBody @Valid IvyRole role){
        log.debug("修改角色方法接收到的入参为:{}",role);
        if (roleService.updateRole(role)) {
            return Result.success("修改角色成功");
        }
        return Result.error(ResultType.USER_INPUT_ERROR,"修改角色失败,入参不符合规则,请检查入参");
    }

    @GetMapping("/delete")
    public Result deleteRoleById(@RequestParam("roleId") Long roleId){
        log.debug("删除角色方法接收到的入参为:{}",roleId);
        if (roleService.deleteRole(roleId)) {
            return Result.success("删除角色成功");
        }
        return Result.error(ResultType.USER_INPUT_ERROR,"删除角色失败,入参不符合规则,请检查入参");
    }
}
